<?php
namespace App\Http\Controllers\admin_system;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class MenuController extends Controller{

    /*
     * 菜单列表
     * */
    public function menu_list(){
        $mainTitle = DB::table('system_menu')->where('menu_type',1)->get();
        foreach($mainTitle as $title){
            $title->subTitle = DB::table('system_menu')->where('parent_id',$title->menu_id)->get();
        }

        //dd($mainTitle);
        return view('admin_system/menu/list',['mainTitle' => $mainTitle]);
    }


    /*
     * 编辑菜单menu_edit_index
     * */
    public function menu_edit_index(Request $request){
        $menuId   = $request->input('menuId',0);
        $mainMenu = DB::table('system_menu')->where('menu_type',1)->get();
        if($menuId > 0){
            $menuInfo = DB::table('system_menu')->where('menu_id',$menuId)->first();
        }else{
            $menuInfo = new \stdClass();
            $menuInfo->menu_id      = 0;
            $menuInfo->menu_name    = '';
            $menuInfo->sort         = 0;
            $menuInfo->parent_id    = 0;
            $menuInfo->link_url     = "";
        }
        return view('admin_system/menu/edit',['mainMenu'=>$mainMenu,'menuInfo'=>$menuInfo]);
    }



    public function menu_edit(Request $request){

        if($request->input('parent_id') == 0){
            $type   = 1;
        }else{
            $type   = 2;
        }

        $menuId = $request->input('menu_id',0);
        $data = [
            'menu_name' => $request->input('menu_name'),
            'menu_type' => $type,
            'link_url'  => $request->input('link_url'),
            'sort'      => $request->input('sort'),
            'parent_id' => $request->input('parent_id'),
            'is_delete' => $request->input('is_delete')
        ];
        if($menuId > 0)
        {
            DB::table('system_menu')->where('menu_id',$menuId)->update($data);
        }
        else
        {
            DB::table('system_menu')->insert($data);
        }
        return $this->menu_list();
    }


    public function close_or_show(Request $request){
        $menuId = $request->input('menuId');
        $db     = DB::table('system_menu')->where('menu_id',$menuId);
        $menuInfo   = $db->first();

        $isDelete   = abs($menuInfo->is_delete-1);
        
        $db->update(['is_delete'=>$isDelete]);
        return redirect()->back();
    }


    /**
     * 彻底删除菜单
     * */
    public function delete_menu(Request $request)
    {
        $menuId = $request->input('menuId');
        DB::table('system_menu')->where('menu_id',$menuId)->delete();
        return redirect()->back();
    }

}